import { colorType, defaultTimingType, sizeType, transfromType } from './index.d';
import useEventQueue from './hooks/useQueue';


// interface IAnimationTask {
//   createAni: (params: IEventParams, options: IEventOptions) => void;
//   register: (...args: any) => number;
// }

export interface IEventParams {
  styleName: sizeType | transfromType | colorType;
  domID: string; 
  to: number;
  eventTime: number;
};

export interface IEventOptions {
  type?: defaultTimingType | { x1:number, y1: number, x2: number, y2: number};
  unit?: string;
  from?: number;
  callback?: () => void;
  auto?: boolean;
  delay?: number;
};


export class AnimationTask {

  private queueFunc: any;
  constructor() {
    this.queueFunc = useEventQueue();
    
  }
  /**
   * 
   * @param {IEventParams} params 
   * @param {IEventOptions} options 
   */
  createAni(params: IEventParams, options: IEventOptions) {
    console.log(params.domID)
    return this.register(
      params.styleName,
      params.domID,
      params.to,
      params.eventTime,
      options.type || 'ease',
      options.unit || 'px',
      options.from || 0,
      options.callback || (() => {}),
      options.auto || false,
      options.delay || 0,
    );
  }

  /**
   * 
   */
  register(
    styleName: sizeType | transfromType | colorType,
    domID: string, 
    to: number,
    eventTime: number,
    type: defaultTimingType | { x1:number, y1: number, x2: number, y2: number},
    unit: string,
    from: number = 0,
    callback: () => void = () => {},
    auto: boolean = false,
    delay: number = 0,
    ): number {
    return this.queueFunc.add({
      id: 0,
      styleName,
      dom: document.querySelector<HTMLElement>(`#${domID}`),
      eventStartTime: Date.now(),
      eventTime,
      timing: typeof type === 'string'? { to, from, type } : { to, from, ...type } ,
      auto,
      delay,
      callback,
      unit,
    });
  }

  cancel(id: number) {
    this.queueFunc.remove(id);
  }

  hasRun(ids: number[]) {
    return ids.some(id => this.queueFunc.isRun(id));
  }
}

export const createAnimationTask = () => {
  return new AnimationTask();
}

